package com.wc.AlgoOJ_train.train2_DP训练.c4.DPE007;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/1/13 2:12
 * @description 最长路径
 * 利用拓扑排序
 * http://43.138.190.70:8888/p/DPE007
 */
public class Main {
    static int maxN = (int) 1e5 + 7;
    //邻接表，key节点，value 能到达的地方
    static HashMap<Integer, ArrayList<Integer>> grid = new HashMap<>();
    // 入度为0的存储
    static ArrayList<Integer> rd0;
    // 每一个位置的入度
    static int[] rds = new int[maxN];
    static boolean[] vis = new boolean[maxN];
    static int n, m;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    // 剩余的节点
    static LinkedList<Integer> re = new LinkedList<>();

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            re.add(i);
            grid.put(i, new ArrayList<>());
        }
        for (int i = 1; i <= m; i++) {
            int u = sc.nextInt();
            int v = sc.nextInt();
            rds[v]++;
            grid.get(u).add(v);
        }

        int ans = 0;
        while (!re.isEmpty()) {
            rd0 = new ArrayList<>();
            Iterator<Integer> it = re.iterator();
            while (it.hasNext()) {
                int u = it.next();
                if (rds[u] == 0) {
                    rd0.add(u);
                    it.remove();
                }
            }
            if (rd0.isEmpty()) {
                break;
            }
            for (Integer u : rd0) {
                vis[u] = true;
                ArrayList<Integer> arr = grid.get(u);
                for (int v : arr) {
                    rds[v]--;
                }
            }
            ans++;
        }
        System.out.println(ans - 1);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }
}